using System;
using System.Collections.Generic;
using System.Diagnostics;

using Clandestine.Input;

namespace Clandestine.Engines.Field
{
	public interface IUpdateable
	{
		void Step(TimeSpan delta, TimeSpan total);
	}
	
	public enum Buttons
	{
		OK,
		Up,
		Down,
		Left,
		Right
	}
	
	public class Logic // right now, trolls through updateables.  could have Ticker functionality added in future!
	{
		public InputManager<Buttons> InputManager { get; private set; }
		
		List<IUpdateable> updateables = new List<IUpdateable>();
		Stopwatch swTotal = new Stopwatch();
		Stopwatch swDelta = new Stopwatch();
		bool frozen = true;
		
		public Logic()
		{
			InputManager = new InputManager<Buttons>();
		}
		
		public void AddUpdateable(IUpdateable iupd)
		{
			lock (updateables)
				updateables.Add(iupd);
		}
		
		public void RemoveUpdateable(IUpdateable iupd)
		{
			lock (updateables)
				updateables.Remove(iupd);
		}
		
		public void Step()
		{
			if (frozen)
			{
				swTotal.Start();
				swDelta.Reset();
				swDelta.Start();
				frozen = false;
				return; // need to get some time in!
			}
			else
			{
				//Console.WriteLine("STEP!");
				TimeSpan de = swDelta.Elapsed, te = swTotal.Elapsed;
				swDelta.Reset();
				swDelta.Start();
				
				lock (updateables)
					foreach (IUpdateable iupd in updateables)
						iupd.Step(de, te);
			}
		}
		
		public void Freeze()
		{
			swTotal.Stop();
			swDelta.Stop();
			frozen = false;
		}
	}
}

